package com.hardydou.system.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.result.Result;
import com.hardydou.common.constant.Const.DeptStatus;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemDept;
import com.hardydou.system.service.ISystemDeptService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020-03-10
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/system/dept")
@Api(tags = "部门管理")
@LogMetaInfo(module = "部门管理")
public class SysDeptController extends BaseController {

    @Autowired
    ISystemDeptService deptService;

    @ApiIgnore
    @GetMapping("")
    public String index() {
        return "dept/list";
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return "dept/add";
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemDept dept, Model model) {
        deptService.addDept(dept);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return "dept/add";
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long deptId, Model model) {
        SystemDept dept = deptService.findById(deptId);
        SystemDept parent = deptService.findById(dept.getParentId());
        model.addAttribute("dept", dept);
        model.addAttribute("parent", parent);
        return "dept/edit";
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(SystemDept dept, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        deptService.update(dept);
        SystemDept parent = deptService.findById(dept.getParentId());
        model.addAttribute("dept", dept);
        model.addAttribute("parent", parent);
        return "dept/edit";
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long deptId, Model model) {
        SystemDept dept = deptService.findById(deptId);
        SystemDept parent = deptService.findById(dept.getParentId());
        model.addAttribute("dept", dept);
        model.addAttribute("parent", parent);
        return "dept/info";
    }

    @ApiIgnore
    @GetMapping("/tree")
    public String tree(long deptId, Model model) {
        SystemDept dept = deptService.findById(deptId);
        model.addAttribute("dept", dept);
        return "dept/tree";
    }


    /* JSON API */

    @ApiOperation(value = "部门列表")
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        List<SystemDept> depts = deptService.findAll(params);
        PageInfo pageInfo = new PageInfo(depts);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加部门")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemDept dept) {
        deptService.addDept(dept);
        return ResultHelper.ok(dept);
    }


    @ApiOperation(value = "删除部门")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(long deptId) {
        deptService.deleteByDeptId(deptId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("deptId", deptId);
        return result;
    }

    @ApiOperation(value = "编辑部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "query")
    })
    @ApiParam(name = "deptId", value = "部门ID", required = true)
    @PostMapping("/update")
    @ResponseBody
    public Result update(SystemDept dept) {
        deptService.update(dept);
        return ResultHelper.ok(dept);
    }

    @ApiOperation(value = "更新状态")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "deptId", value = "部门ID", required = true)
    )
    @PostMapping("/status")
    @ResponseBody
    public Result status(long deptId) {
        SystemDept dept = deptService.findById(deptId);
        dept.setStatus(dept.getStatus() == DeptStatus.NORMAL.val ? DeptStatus.LOCKED.val : DeptStatus.NORMAL.val);
        deptService.update(dept);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("resId", deptId);
        result.getData().put("status", dept.getStatus());
        return result;
    }

}
